<?php
namespace app\index\controller;
use \think\Controller;
use \think\Loader;
use \think\captcha\Captcha;
use alimsg\Msg;
use app\index\model\User;

class Reg extends Common
{

	public function test()
	{
		$res = Msg::regMsg('18925139194',8989);

		var_dump($res);
	}
    public function index()
    {
    	$this->assign('title',"注册");
    	return $this->fetch();
    }

    public function getcode()
    {
    	$config =    [   
		    // 验证码位数
		    'length'      => 3,
		];
    	$captcha = new Captcha($config);
		return $captcha->entry();
    }

    public function sendmsg()
    {
    	$this->success("发送成功，请查收 ");
    	$data =  input('post.');
    	// 加载验证器
    	$validate = Loader::validate('reg');
    	// 判断数据是否通过
		if(!$validate->scene('msg')->check($data)){
		    $this->error($validate->getError());
		}

		$num = mt_rand(1000,9999);

		$data['ctime']=time();

		session('msginfo',$data);

		$res = Msg::regMsg($data['phone'],$num);

		if($res->Message=="OK"){
			$this->success("发送成功，请查收 ");
		}else{
			$this->error("发送失败，请重试");
		}
    }

    public function addUser()
    {
    	// 接数据 
    	$data = input('post.');
    	// 加载验证器
    	$validate = Loader::validate('reg');
    	// 判断数据是否通过
		if(!$validate->check($data)){
		    $this->error($validate->getError());
		}

		// 调用一个模型
		$user = new User;

		// 处理一下数据 
		$data['ctime'] = time();

		$data['password'] = md5($data['password']);

		$user->data($data);
		// 把数据插入数据 库
		$res = $user->allowField(true)->save();

		if($res){
			$this->success("注册成功");
		}else{
			$this->error("注册失败");
		}

    }
}
